package com.lkxicy.wos.mapper;

import com.lkxicy.wos.pojo.entity.Holiday;
import com.lkxicy.wos.pojo.entity.Schedule;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lkxicy.wos.pojo.vo.ScheduleRecord;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lkxicy
 * @since 2020-12-02
 */
public interface ScheduleMapper extends BaseMapper<Schedule> {

    @Select("SELECT staff.staffNumber,staff.name,s.beginTime,s.endTime,s.workContent\n" +
            "FROM `schedule` s,staff\n" +
            "WHERE staff.staffNumber=#{staffNumber} \n" +
            "AND staff.staffNumber=s.staffNumber \n" +
            "AND (NOW()<s.beginTime OR NOW()<s.endTime) ")
    public List<ScheduleRecord> findOneAllNowSchedule(@Param("staffNumber") String staffNumber);

    @Select("SELECT staff.staffNumber,staff.name,s.beginTime,s.endTime,s.workContent\n" +
            "FROM `schedule` s,staff \n" +
            "WHERE staff.staffNumber=s.staffNumber \n" +
            "AND (\n" +
            "    (YEAR(s.beginTime)=YEAR(#{searchTime}) \n" +
            "AND MONTH(s.beginTime)=MONTH(#{searchTime}) \n" +
            "AND DAY(s.beginTime)=DAY(#{searchTime}) ) \n" +
            "OR \n" +
            "    (YEAR(s.endTime)=YEAR(#{searchTime}) \n" +
            "AND MONTH(s.endTime)=MONTH(#{searchTime}) \n" +
            "AND DAY(s.endTime)=DAY(#{searchTime}) ) \n" +
            ")")
    public List<ScheduleRecord> searchSomedaySchedule(@Param("searchTime") Date searchTime);

    @Select("select * from `schedule` where staffNumber=#{staffNumber} and " +
            "((beginTime<=#{endTime} and endTime>=#{endTime}) or" +
            "(beginTime<=#{beginTime} and endTime>=#{beginTime}) or" +
            "(beginTime>=#{beginTime} and endTime<=#{endTime}))")
    public List<Holiday> findOneScheduleRecord(@Param("staffNumber") String staffNumber,
                                              @Param("beginTime")Date beginTime,
                                              @Param("endTime") Date endTime);

    @Select("SELECT * \n" +
            "FROM `schedule` \n" +
            "WHERE staffNumber=#{staffNumber}\n" +
            "AND (\n" +
            "    (YEAR(beginTime)=YEAR(#{workTime}) \n" +
            "AND MONTH(beginTime)=MONTH(#{workTime}) \n" +
            "AND DAY(beginTime)=DAY(#{workTime}) ) \n" +
            "OR \n" +
            "    (YEAR(endTime)=YEAR(#{workTime}) \n" +
            "AND MONTH(endTime)=MONTH(#{workTime}) \n" +
            "AND DAY(endTime)=DAY(#{workTime}) ) \n" +
            ")")
    public Schedule findOneSchedule(@Param("staffNumber") String staffNumber,
                                         @Param("workTime") Date workTime);

    @Select("SELECT staff.staffNumber,staff.name,s.beginTime,s.endTime,s.workContent \n" +
            "FROM `schedule` s,staff \n" +
            "WHERE (\n" +
            "    (YEAR(beginTime)=YEAR(#{workTime}) \n" +
            "AND MONTH(beginTime)=MONTH(#{workTime}) \n" +
            "AND DAY(beginTime)=DAY(#{workTime}) ) \n" +
            "OR \n" +
            "    (YEAR(endTime)=YEAR(#{workTime}) \n" +
            "AND MONTH(endTime)=MONTH(#{workTime}) \n" +
            "AND DAY(endTime)=DAY(#{workTime}) ) \n")
    public List<ScheduleRecord> findOneDayAllSchedule(@Param("workTime") Date workTime);

    @Delete("DELETE \n" +
            "FROM `schedule` \n" +
            "WHERE staffNumber=#{staffNumber} \n" +
            "AND (\n" +
            "    (YEAR(beginTime)=YEAR(#{workTime}) \n" +
            "AND MONTH(beginTime)=MONTH(#{workTime}) \n" +
            "AND DAY(beginTime)=DAY(#{workTime}) ) \n" +
            "OR \n" +
            "    (YEAR(endTime)=YEAR(#{workTime}) \n" +
            "AND MONTH(endTime)=MONTH(#{workTime}) \n" +
            "AND DAY(endTime)=DAY(#{workTime}) ) \n" +
            ")")
    public void deleteSchedule(@Param("staffNumber") String staffNumber,
                               @Param("workTime") Date workTime);

}
